<?php
namespace App\Extend\Wx;

use App\Dao\WxdataDao;
use App\Server\CurlApi;
use App\Extend\Wx\Wxlogin as WxloginExtend;
use App\Utils\ServerUtil;

class Wxinfo
{

    /**
     * 获取用户访问小程序数据概况 查询一天的数据 转发数据 ok
     * @param string $begin_date 搜索日期 例如 20200520
     * @return array 访问总数 visit_total 分享次数 share_pv 分享人数  share_uv 留存json数据
     * Abell 2020/5/12
     */

    public function daily_summary(){
        $begin_date = date('Ymd',strtotime('-1 day'));
        $wx_request = ['begin_date' => $begin_date,'end_date' => $begin_date];
        // 定时任务加锁
        if(ServerUtil::getLock('daily_summary2',10)){
            // 获取分享统计数量
            $share_result_array = $this->weixinShareGet($wx_request);
            // 获取日留存数据
            $retain_result = $this->weixinRetainGet($wx_request);

            $update_data = [
                'visit_total' => (int)$share_result_array['visit_total'],
                'share_pv' => (int)$share_result_array['share_pv'],
                'share_uv' => (int)$share_result_array['share_uv'],
            ];

            $update_data['daily_retain'] = $retain_result;

            //存储数据 写入数据库 新增或者更新
            $result = count($update_data) ? make(WxdataDao::class)->newOrUpdate($begin_date,$update_data) : false;
            ServerUtil::delLock('daily_summary2');
            return  ['code'=>0,'msg'=>$result];
        }else{
            var_dump('获取微信后台统计数据失败');
            return ['code'=>400,'msg'=>'获取微信后台统计数据失败'];
        }
    }

    // 请求分享数据
    private function weixinShareGet($wx_request){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        // 获取分享统计数量
        $share_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token='.$data;
        $share_result = make(CurlApi::class)->jsonpost($share_url,$wx_request);
        $share_result_array = json_decode($share_result,true);
        if(array_key_exists('list',$share_result_array)){
            var_dump('微信请求分享数据成功');
            var_dump(date('Y-m-d H:i:s'));
            var_dump($share_result_array);
            return $share_result_array['list'][0];
        }else{

            var_dump('微信请求分享数据失败,等30秒后重新请求');
            var_dump($share_result);
            var_dump(date('Y-m-d H:i:s'));
            sleep(30);
            $this->weixinShareGet($wx_request);
        }
    }

    // 微信请求日留存数据
    private function weixinRetainGet($wx_request){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $retain_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token='.$data;
        $retain_result = make(CurlApi::class)->jsonpost($retain_url,$wx_request);
        $retain_result_array = json_decode($retain_result,true);
        if(array_key_exists('visit_uv_new',$retain_result_array)){
            return $retain_result;
        }else{
            var_dump('微信请求日留存数据失败,等30秒后重新请求');
            var_dump($retain_result);
            var_dump(date('Y-m-d H:i:s'));
            sleep(30);
            $this->weixinRetainGet($wx_request);
        }
    }

    public function daily_summary2(){
        $begin_date = date('Ymd',strtotime('-1 day'));
        $wx_request = ['begin_date' => $begin_date,'end_date' => $begin_date];
        // 定时任务加锁
        if(ServerUtil::getLock('daily_summary',86500)){
            $data = make(WxloginExtend::class)->check_token_redis(false);
            // 获取分享统计数量
            $share_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token='.$data;
            $share_result = make(CurlApi::class)->jsonpost($share_url,$wx_request);
            $share_result_array = json_decode($share_result,true);
            // 获取日留存数据
            $retain_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token='.$data;
            $retain_result = make(CurlApi::class)->jsonpost($retain_url,$wx_request);
            $retain_result_array = json_decode($retain_result,true);
            $update_data = array();
            if(count($share_result_array['list'])){
                $share_result_array_data = $share_result_array['list'][0];
                $update_data = [
                    'visit_total' => (int)$share_result_array_data['visit_total'],
                    'share_pv' => (int)$share_result_array_data['share_pv'],
                    'share_uv' => (int)$share_result_array_data['share_uv'],
                ];
            }
            if(count($retain_result_array['visit_uv_new'])){
                $update_data['daily_retain'] = $retain_result;
            }
            //存储数据 写入数据库 新增或者更新
            $result = count($update_data) ? make(WxdataDao::class)->newOrUpdate($begin_date,$update_data) : false;
            ServerUtil::delLock('daily_summary');
            return  ['code'=>0,'msg'=>$result];
        }else{
            var_dump('获取微信后台统计数据失败');
            return ['code'=>400,'msg'=>'获取微信后台统计数据失败'];
        }
    }

    /**
     * 获取用户访问小程序数据概况 查询一天的数据 转发数据 ok
     * @param string $begin_date 搜索日期 例如 20200520
     * @return
     * 访问总数 visit_total 分享次数 share_pv 分享人数  share_uv 留存json数据
     * Abell 2020/5/12
     */
    public function daily($begin_date=null){
        $begin_date = $begin_date ? $begin_date : date('Ymd',strtotime('-1 day'));
        $wx_request = ['begin_date' => $begin_date,'end_date' => $begin_date];
        $data = make(WxloginExtend::class)->check_token_redis(false);
        // 获取分享统计数量
        $share_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token='.$data;
        $share_result = make(CurlApi::class)->jsonpost($share_url,$wx_request);
        $share_result_array = json_decode($share_result,true);
        // 获取日留存数据
        $retain_url = 'https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token='.$data;
        $retain_result = make(CurlApi::class)->jsonpost($retain_url,$wx_request);
        $retain_result_array = json_decode($retain_result,true);
        $update_data = array();
        if(count($share_result_array['list'])){
            $share_result_array_data = $share_result_array['list'][0];
            $update_data = [
                'visit_total' => (int)$share_result_array_data['visit_total'],
                'share_pv' => (int)$share_result_array_data['share_pv'],
                'share_uv' => (int)$share_result_array_data['share_uv'],
            ];
        }
        if(count($retain_result_array['visit_uv_new'])){
            $update_data['daily_retain'] = $retain_result;
        }
        //存储数据 写入数据库 新增或者更新
        $result = count($update_data) ? make(WxdataDao::class)->newOrUpdate($begin_date,$update_data) : false;
        ServerUtil::delLock('daily_summary');
        return  ['code'=>0,'msg'=>$result];
    }
}